package mi.payment.transaction.authorize.complete;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.TransactionInputPwdOperation;
import mi.payment.transaction.V2InputPasswordActivity;
import mi.payment.ui.base.PageRoute;
import mi.payment.utils.MoneyUtils;
import mi.payment.utils.transaction.PreAuthorizationCompleteTask;
import sunmi.payment.ui.dialog.LoadDialog;

/**
 * 预授权完成模块 -- 输入密码界面
 *
 * @author longx on 2017/3/5.
 */
public class PreAuthorizeCompleteInputPWDActivity extends V2InputPasswordActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        startTransactionProcess();
    }

    private void initView() {
        mTvTitle.setText(R.string.pre_auth_complete);

        String amountStr = getIntent().getStringExtra("extra_amount");
        long amount = MoneyUtils.stringMoney2LongCent(amountStr);
        mTradeData.getPayDetail().setAmount(amount);

        // 预授权完成是否输入密码
        boolean isFieldAuthCompleteReqPwd = TransactionInputPwdOperation.getInstance().getTransactionInputPwd().isFieldAuthCompleteReqPsd();
        setForceInputPWD(isFieldAuthCompleteReqPwd);
    }

    @Override
    @SuppressLint("StaticFieldLeak")
    public void onStartOnline() {
        // 预授权完成
        mTradeData.getPayDetail().setTransactionType(6);
        PreAuthorizationCompleteTask task = new PreAuthorizationCompleteTask(this) {

            @Override
            protected void onPostExecuteImpl(TradeData tradeData) {
                PayDetail resPay = tradeData.getPayDetail();
                PageRoute.startAction(mActivity, PageRoute.TYPE_PRE_AUTH_COMPLETE, resPay);
            }

            @Override
            protected void onTaskTimeoutImpl() {
                PageRoute.startTimeoutAction(mActivity, PageRoute.TYPE_PRE_AUTH_COMPLETE, mTradeData.payDetail);
            }

        };
        LoadDialog loadingDialog = getLoadingDialog(R.string.load_connecting);
        task.setLoadDialog(loadingDialog);
        task.execute(mTradeData);
    }

    public static void startAction(Context context, String amount) {
        Intent intent = new Intent(context, PreAuthorizeCompleteInputPWDActivity.class);
        intent.putExtra("extra_amount", amount);
        context.startActivity(intent);
    }


}
